package org.builder.main;

import org.annotation.ExcelColumn;
import org.apache.poi.ss.usermodel.Cell;
import org.apache.poi.ss.usermodel.Row;
import org.apache.poi.ss.usermodel.Sheet;
import org.apache.poi.ss.usermodel.Workbook;
import org.apache.poi.xssf.streaming.SXSSFWorkbook;
import org.pojo.TimeSegmentCustomerFlowAnalysisBean;
import org.util.MethodUtils;
import org.util.ScanExcelBeanTool;

import java.io.File;
import java.io.FileOutputStream;
import java.lang.reflect.Method;
import java.util.*;

/**
 * @author huangshen
 * @version 0
 * @description SXSSFTest
 * @since 2022/2/9 20:21
 */
public class SXSSFTest {

    public static void main(String[] args) throws Exception{

        List<TimeSegmentCustomerFlowAnalysisBean> beanList = new ArrayList<>();
        Class<TimeSegmentCustomerFlowAnalysisBean> targetClass = TimeSegmentCustomerFlowAnalysisBean.class;

        List<Method> methodList = new MethodUtils(targetClass).getSetMethodListByAnnotation(ExcelColumn.class);
        int index = 10000;
        TimeSegmentCustomerFlowAnalysisBean bean = new TimeSegmentCustomerFlowAnalysisBean();
        while (index > 0) {
            TimeSegmentCustomerFlowAnalysisBean clone = (TimeSegmentCustomerFlowAnalysisBean) bean.clone();
            for (Method method : methodList) {
                method.invoke(clone, "1");
            }
            index--;
            beanList.add(clone);
        }
        // 获取get方法集合
        List<Method> getMethodList = new MethodUtils(targetClass).getGetMethodListByAnnotation(ExcelColumn.class);
        // 获取列名集合
        List<String> columnNameList = new ScanExcelBeanTool(targetClass).registerExcelColumnName();
        // 创建一个工作簿
        Workbook workbook = new SXSSFWorkbook(100);
        // 创建一个工作表sheet
        Sheet sheet = workbook.createSheet();
        // 创建第一行
        Row row = sheet.createRow(0);
        // 创建一个单元格
        Cell cell = null;
        // 创建表头
        for (int i = 0; i < columnNameList.size(); i++) {
            cell = row.createCell(i);
            cell.setCellValue(columnNameList.get(i));
        }
        // 从第二行开始追加数据
        for (int i = 1; i < beanList.size(); i++) {
            // 创建第i行
            Row nextRow = sheet.createRow(i);
            Cell cell2;
            // 参数代表第几列
            for (int mm = 0; mm < getMethodList.size(); mm++) {
                cell2 = nextRow.createCell(mm);
                // 设置单元格参数
                cell2.setCellValue(String.valueOf(MethodUtils.getValue(getMethodList.get(mm), beanList.get(i-1))));
            }
            System.out.println(i);
        }
        // 创建一个文件
        File file = new File("C:/Users/gugu/Desktop/test1/" +new Random().nextInt() + ".xlsx");
        try(
            FileOutputStream outputStream = new FileOutputStream(file);
        ) {
            workbook.write(outputStream);
        } catch (Exception e) {
            e.printStackTrace();
        } finally {
            workbook.close();
        }

    }
}
